package cn.itcast.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * @author : AdinZ  1454269@qq.com
 * @Project: Common
 * @Package com.xianxian.utils
 * @Description: TODO
 * @date Date : 2018年12月01日 13:58
 **/

public class DateUtils {
    private static Logger logger= LoggerFactory.getLogger(DateUtils.class);
    private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);

    private static TimeZone timeZone = TimeZone.getTimeZone("GMT+0800");


    /***
     * 自定义时间,格式, 格式化
     * @param date
     * @param format
     * @return
     */
    public static String format(java.util.Date date, String format){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(timeZone);
        return simpleDateFormat.format(date);
    }
    public static String format(long time,String format){
        return format(new java.util.Date(time),format);
    }
    public static String format(String format){
        return format(new java.util.Date(),format);
    }

    public static java.util.Date parse(String date, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(timeZone);
        return simpleDateFormat.parse(date);
    }


    /***
     * 当前时间格式化
     * @return
     */
    public static String format(){
       return  format(new java.util.Date());
    }

    /**
     * 时间格式化 2018-11-11 12:12:12
     * @param date
     * @return
     */
    public static String format(java.util.Date date){
        format.setTimeZone(timeZone);
        return  format.format(date);
    }
    public static String format(long time){
        return format(new java.util.Date(time));
    }

    /***
     * 获取年月 2018-11
     * @param date 时间
     * @return 2018-11
     */
    public static String formatYearMonth(java.util.Date date){
        return format(date,"yyyy-MM-dd");
    }

    /**
     * 获取当前时间的年月
     * @return
     */
    public static String formatYearMonth(){
        return formatYearMonth(new java.util.Date());
    }


    public static java.util.Date todayStart(int day){
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTimeZone(timeZone);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        currentDate.add(Calendar.DATE, day);
        return currentDate.getTime();
    }

    public static java.util.Date todayStart(java.util.Date date){
        Calendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.setTimeZone(timeZone);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        return currentDate.getTime();
    }
    public static java.util.Date todayEnd(java.util.Date date){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static java.util.Date todayEnd(int day){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.add(Calendar.DATE, day);
        java.util.Date date=calendar.getTime();
        return date;
    }

    public static java.util.Date getDate(){
        Calendar cal = Calendar.getInstance(timeZone);
        return cal.getTime();
    }
    public static long getTime(){
        return getDate().getTime();
    }

    public static java.util.Date getMonthStart(int month){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY)+month, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    public static java.util.Date getMonthEnd(int month) {
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY)+month, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static java.util.Date getYearStart(int year){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR)+year, cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
    public static java.util.Date getYearEnd(int year){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR)+year, cal.getActualMaximum(Calendar.MONDAY), cal.getActualMaximum(Calendar.DAY_OF_MONTH), 0, 0, 0);
//        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    public static java.util.Date toDate(String date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(timeZone);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static java.util.Date toDate(int day){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.add(Calendar.DATE, day);
        java.util.Date date=calendar.getTime();
        return date;
    }
    public static java.util.Date toDateYear(java.util.Date date, int year){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR,year);
        return calendar.getTime();
    }

    public static java.util.Date toDateMonth(java.util.Date date, int month){
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,month);
        return calendar.getTime();
    }
    public static java.util.Date toDateDay(java.util.Date date, int day) {
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTime(date);
        calendar.add(Calendar.DATE,day);
        return calendar.getTime();
    }

    public static java.util.Date toDate(String date){
        return toDate(date,"yyyy-MM-dd HH:mm:ss");
    }


    public static long addDay(int day) {
       return DateUtils.day(day).getTime();
    }
    public static java.util.Date day(int day){
        Calendar cal = Calendar.getInstance(timeZone);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH)+day, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
        return cal.getTime();
    }

    public static int getWeekOfDate(){
        return getWeekOfDate(new java.util.Date());
    }

    public static int getWeekOfDate(java.util.Date date){
        Calendar cal=new Calendar.Builder().setInstant(date).setTimeZone(timeZone).build();
        return cal.get(Calendar.DAY_OF_WEEK)-1;
    }

    public static boolean isWorkTime(java.util.Date date){
        int week=getWeekOfDate(date);
        Calendar cal=new Calendar.Builder().setInstant(date).build();
        cal.setTimeZone(timeZone);
        int hour= cal.get(Calendar.HOUR_OF_DAY);
        logger.info("判断工作日今天星期"+week+"\t\t"+format(cal.getTime())+"\t 现在" +hour+"点");
        if(week==0||week==6){
            return false;
        }else {
            if(hour<9||hour>=17) return false;
            else return true;
        }
    }

    public static long getNextWeekTuesDay(java.util.Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(Calendar.TUESDAY);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day+7);
        return calendar.getTimeInMillis();
    }

    public static void main(String[] age) {
       // System.out.println(DateUtils.format(toDateYear(new Date(),1)));
        System.out.println(DateUtils.format(toDateDay(new java.util.Date(),1)));
    }


    public static long getTimestamp() {
        String timestamp=new Date().getTime()+"";
        return Long.parseLong(timestamp.substring(0,10));
    }

    public static long getToDayHour(int hour) {
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }


}
